<?php

class NoticeController extends Controller
{
	public $layout='notice';

	private $_model;
	
	/**
	 * 
	 */
	public function actionIndex()
	{
		$_nt_type = CHtml::encode($_GET['nt']);
		$_ntxmodel = DTable::model('{{users_notice}}')->findAll(array(
									'condition'=>'to_id=:to_id and isread=:isread',
									'params'=>array(':to_id'=>Yii::app()->user->getId(),':isread'=>'0'),
		));
		$this->render('/notice/notice',array('_ntxmodel'=>$_ntxmodel,'_notice_type'=>'addfriendrequest'));
	}

	/**
	 * 通过不同的类型获取数据
	 */
	public function actionGetNt()
	{
		$_nt_type = CHtml::encode($_GET['nt']);
		$_ntxmodel = DTable::model('{{users_notice}}')->findAll(array(
									'condition'=>'to_id=:to_id and notice_type=:notice_type',
									'params'=>array(':to_id'=>Yii::app()->user->getId(),':notice_type'=>$_nt_type),
		));
		switch ($_nt_type)
		{
			case "addfriendrequest":
				$_title = '加熟人请求处理';
				break;
				
			case "addfriendresultrequest":
				$_title = '请求结果';
				break;
		}
		$_nthtml = $this->renderPartial('/notice/notice',array('_ntxmodel'=>$_ntxmodel,'_title'=>$_title,'_notice_type'=>$_nt_type),true);
		echo CJSON::encode(array('_html'=>$_nthtml));
	}
	
	/*
	 * 同意添加好友
	 */
	public function actionAgreeAddFriend()
	{
		$_id = CHtml::encode($_GET['id']);
		if($_id < 1)
			exit;
			
		//检测是不是合法
		$_agreeModel = DTable::model('{{users_notice}}')->findByPk($_id);
		if($_agreeModel->to_id !== Yii::app()->user->getId())
			exit;

		$this->InsertNotice(Yii::app()->user->getId(), $_agreeModel->from_id, 'addfriendresultrequest', $this->_userProfileModel->nickname.' 同意成为你的熟人');
	}
	
	public function InsertNotice($from_id, $to_id,$notice_type ,$content, $other='')
	{
		//发送通知
		$_noticemodel = new DTable('{{users_notice}}');
		$_noticemodel->from_id = $from_id;
		$_noticemodel->to_id = $to_id;
		$_noticemodel->notice_type = $notice_type;
		$_noticemodel->content = $content;
		$_noticemodel->other = $other;
		$_noticemodel->createdate = date('Y-m-d H:i:s');
		$_noticemodel->save();
	}
}